const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    by_qr: 1
  },


  onLoad: function (options) {
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.geneal
    })
    const phone = wx.getStorageSync('phone')
    if (phone) {
      wx.navigateTo({
        url: '../choujiang/choujiang',
      })
    }
    if (options.scene) {
      let constscene = decodeURIComponent(options.scene)
      wx.setStorageSync('scene', constscene)
      console.log('获取二维码推广信息');
    }
    if (options.by_qr) {
      that.setData({
        by_qr: options.by_qr
      })
    }
  },

  getPhoneNumber(e) {
    var that = this;
    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {
      that.setData({
        mobile: res.result,
      })
      wx.setStorageSync('phone', res.result)
      console.log(res.result)
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/choujiang/register?actid=38495&callback=?', //接收url
        data: {
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }

          wx.navigateTo({
            url: '../choujiang/choujiang',
          })

          let constscene = wx.getStorageSync('scene')
          if (constscene) {
            let phone = wx.getStorageSync('phone')
            wx.request({
              url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/get_we_chat_scene_code',
              method: 'POST',
              data: {
                scene: constscene,
                extra: {
                  phone: that.data.mobile,
                  fenxiao: that.data.area,
                  geneal: that.data.geneal,
                },
                by_qr: that.data.by_qr,
                mobile: that.data.mobile
              },
              success: res => {
                console.log(res);
              }
            })
          }
          // console.log(theJson)

        }
      })
    }).catch(err => {
      console.error(err);
    });
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // /**
  //  * 用户点击右上角分享
  //  */
  // onShareAppMessage: function () {
  //   return {
  //     title: '端午抽奖活动',
  //     path: 'pages/index/index?scene=' + sence + '&by_qr=2'
  //   }
  // },
  // /**分享到朋友圈*/
  // onShareTimeline: function () {
  //   var that = this;
  //   var sence = wx.getStorageSync('scene')
  //   return {
  //     title: '端午抽奖活动',
  //     path: 'pages/index/index?scene' + sence + '&by_qr=2'
  //   }
  // }
})